#!/bin/sh

# THIS VERSION IS BUGGED - IT NEEDS FIXES TO RUN, SORRY #

# Release - 03/07 #

############################ SupTime Project v0.2a ###############################
# Hi, this is really-easy bash script which generates png image with your system #
# top and current uptime.                                                        #
##################################################################################
# Please consider that it is an early alpha version of script which I'm writing  #
# _FOR_PERSONAL_USE_ - so don't wait anything great. Also my linux knowledge is  #
# ather poor, so it is sure that there are many things in this script which      #
# could be improved...                                                           #
##################################################################################
# Script requires:                                                               #
# - ImageMagick >= 6.2.5                                                         #
# - uptimed                                                                      #
# - uprecords (uprecords-cgi)                                                    #
##################################################################################
# Contact? >> sufixx[at]gmail.com                                                #
##################################################################################


# Configurable variables
BASE_DIR=`pwd` # of course - put here normal path!
OUT=$BASE_DIR/out.png
THEME=theme_alpha

# No need to change
TMP_DIR=/tmp
THEME_DIR=$BASE_DIR/
BACKGROUND=$THEME_DIR/$THEME/bg.png
FOREGROUND=$THEME_DIR/$THEME/fg.png

# Read uptime variable from uprecords
UPTIME=`uprecords -a | grep "\->" | awk '{print $3 $4 $5 }' | sed 's/\days/ days/' |sed 's/\,/ /'`

# Read top uptime variable from uprecords
TOP=`uprecords | head -n 3 | tail -n 1 | awk '{print $2 $3 $4}' | sed 's/\days/ days/' |sed 's/\,/ /'`

# Check if theme exists
if [ -e $THEME_DIR/$THEME/theme.conf ]; then
	source $THEME_DIR/$THEME/theme.conf
else
	echo " - Theme not found! Check if it really exists."
	exit;
fi

Load text layout of chosen theme
if [ -e $THEME_DIR/$THEME/layout.conf ]; then
	source $THEME_DIR/$THEME/layout.conf
else
	echo " - Text layout config not found! Check if it really exists."
	exit;
fi

# Software checks.
# Check if uprecords is installed
if [ -e /usr/bin/uprecords ]; then
	cat /dev/null
else
	echo " - Please install uprecords program."
	exit;
fi

# Check if ImageMagick is installed
IMAGEMAGICK=`identify --version |head -n 1| awk {'print $3'}| sed 's/\.//' | sed 's/\.//'`

if [ $IMAGEMAGICK -ge 625 ]; then
	cat /dev/null
else
	echo " - Your ImageMagick version is older then 6.2.5"
	exit;
fi

# Render image with text from text file
cat $TMP_DIR/up.txt | convert -background transparent -pointsize $FONT_SIZE -fill $FONT_COLOR -font $FONT label:@- -append $TMP_DIR/text.png

# Merge graphical background, text image and foreground 
composite $TMP_DIR/text.png -compose Over $BACKGROUND $TMP_DIR/compisite.png
if [ -e $THEME_DIR/$THEME/extra.conf ]; then	# Check if file with extra composite commands exists 
	source $THEME_DIR/$THEME/extra.conf
	echo " + Adding extra effects..."
fi
composite $FOREGROUND -compose Over $TMP_DIR/compisite.png $BASE_DIR/out.png

# Cleanup
rm -f $TMP_DIR/compisite.png $TMP_DIR/up.txt $TMP_DIR/text.png

# Upload image -- not yet - scp? ntftp?

exit;
